<%@ page import="db.DBConnection" %>
<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 07.05.12
  Time: 19:26
  To change this template use File | Settings | File Templates.
--%>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "index.jsp";
        response.sendRedirect(redirectURL);
        return;
    }
    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Send message</title>
    <link rel="stylesheet" type="text/css" href="style"/>
    <%
        String message = request.getParameter("message");
        boolean isOk = false;
        boolean hasParams = false;
        if (message != null) {
            hasParams = true;
            isOk = dbConnection.doSendMessage(user_id,25,message);
        }
    %>
</head>
<body>
<div class="logout" style="text-align:right;"><a href="logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">

        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><a href="accounts.jsp">Accounts</a></li>
            <li><a href="operations.jsp">Operations</a></li>
            <li><b><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></b></li>
        </ul>
    </div>
    <div class="content">
        <form method="post" action="send_message.jsp">
            <table>
                <%if (message!=null) { %>
                <tr><textarea cols="30" rows="5" name="message"><%=message%></textarea></tr>
                <% } else {%>
                <tr><textarea cols="30" rows="5" name="message"></textarea></tr>
                <% } %>
                <tr>
                    <td><input type="submit" value="Send message"></td>
                    <% if (hasParams) {
                        if (isOk) {
                            %> <td style="color: green;"> Message sended! </td> <%
                        } else{
                            %> <td style="color: red;"> Sending error! Try later! </td><%
                        }
                    } %>
                </tr>
            </table>
        </form>
    </div>
</div>
</body>
</html>